package com.ruoyi.project.mall.api;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.mall.service.MallCompanyUserService;
import com.ruoyi.project.mall.service.MallShopUserService;
import com.ruoyi.project.wx.service.MemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 个人中心
 */
@Controller
@RequestMapping("/api/user")
public class ApiUserController {
    @Autowired
    private MemberUserService memberUserService;
    @Autowired
    private MallCompanyUserService mallCompanyUserService;
    @Autowired
    private MallShopUserService mallShopUserService;


    /***
     * 删除采购人信息
     * @param params
     * @return
     */
    @PostMapping( "/deleteCompanyUser")
    @ResponseBody
    public R deleteCompanyUser(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("companyUserId") || StringUtils.isBlank(params.get("companyUserId").toString())
            ) {
                return R.error("参数错误");
            }
            return  memberUserService.deleteCompanyUser(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }



    /**
     * 企业用户修改采购人信息
     * @param params
     * @return
     */
    @PostMapping( "/updateCompanyUserInfo")
    @ResponseBody
    public R updateCompanyUserInfo(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("companyUserId") || StringUtils.isBlank(params.get("companyUserId").toString())
                    ||!params.containsKey("name") || StringUtils.isBlank(params.get("name").toString())
                    ||!params.containsKey("parentId") || StringUtils.isBlank(params.get("parentId").toString())
                    ||!params.containsKey("level") || StringUtils.isBlank(params.get("level").toString())



            ) {
                return R.error("参数错误");
            }
            return  memberUserService.updateCompanyUserInfo(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 获取采购人信息
     * @param params
     * @return
     */
    @PostMapping( "/getCompanyUserInfo")
    @ResponseBody
    public R getCompanyUserInfo(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("companyUserId") || StringUtils.isBlank(params.get("companyUserId").toString())

            ) {
                return R.error("参数错误");
            }
            return  memberUserService.getCompanyUserInfo(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /***
     *  修改采购人密码
     * @param params
     * @return
     */
    @PostMapping( "/updateCompanyUserPassword")
    @ResponseBody
    public R updateCompanyUserPassword(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("companyUserId") || StringUtils.isBlank(params.get("companyUserId").toString())
                    ||!params.containsKey("password") || StringUtils.isBlank(params.get("password").toString())

            ) {
                return R.error("参数错误");
            }
            return  memberUserService.updateCompanyUserPassword(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 获取采购人列表
     * @param params
     * @return
     */
    @PostMapping( "/getCompanyUserList")
    @ResponseBody
    public R getCompanyUserList(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())

            ) {
                return R.error("参数错误");
            }
            return  memberUserService.getCompanyUserList(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /**
     * 获取采购员以上用户用户
     * @param params
     * @return
     */
    @PostMapping( "/getUpUser")
    @ResponseBody
    public R getUpUser(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())




            ) {
                return R.error("参数错误");
            }
            return  memberUserService.getUpUser(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 企业用户添加采购人
     * @param params
     * @return
     */
    @PostMapping( "/addCompanyUser")
    @ResponseBody
    public R addCompanyUser(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())
                    ||!params.containsKey("password") || StringUtils.isBlank(params.get("password").toString())
                    ||!params.containsKey("name") || StringUtils.isBlank(params.get("name").toString())
                    ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                    ||!params.containsKey("parentId") || StringUtils.isBlank(params.get("parentId").toString())
                    ||!params.containsKey("level") || StringUtils.isBlank(params.get("level").toString())



            ) {
                return R.error("参数错误");
            }
            return  memberUserService.addCompanyUser(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /**
     * 忘记密码
     * @param params
     * @return
     */
    @PostMapping( "/forgetPassword")
    @ResponseBody
    public R forgetPassword(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())
                    ||!params.containsKey("newPassword") || StringUtils.isBlank(params.get("newPassword").toString())
                    ||!params.containsKey("code") || StringUtils.isBlank(params.get("code").toString())



            ) {
                return R.error("参数错误");
            }
            return  memberUserService.forgetPassword(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /***
     * 修改密码
     * @param params
     * @return
     */

    @PostMapping( "/updateUserPassword")
    @ResponseBody
    public R updateUserPassword(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())
                    ||!params.containsKey("oldPassword") || StringUtils.isBlank(params.get("oldPassword").toString())
                    ||!params.containsKey("newPassword") || StringUtils.isBlank(params.get("newPassword").toString())
                    ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())



            ) {
                return R.error("参数错误");
            }
            return  memberUserService.updateUserPassword(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /**
     * 修改用户信息
     * @param params
     * @return
     */
    @PostMapping( "/updateUser")
    @ResponseBody
    public R updateUser(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                    ||!params.containsKey("avator") || StringUtils.isBlank(params.get("avator").toString())
                    ||!params.containsKey("name") || StringUtils.isBlank(params.get("name").toString())
                    ||!params.containsKey("sex") || StringUtils.isBlank(params.get("sex").toString())


            ) {
                return R.error("参数错误");
            }
            return  memberUserService.updateUser(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 获取用户基本信息
     * @param params
     * @return
     */
    @PostMapping( "/getUserInfo")
    @ResponseBody
    public R getUserInfo(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
            ) {
                return R.error("参数错误");
            }
            return  memberUserService.getUserInfo(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
    /**
     *
     * @param params 普通用户注册
     * @return
     */
    @PostMapping( "/memberUser/register")
    @ResponseBody
    public R memberUserRegister(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())
            ||!params.containsKey("password") || StringUtils.isBlank(params.get("password").toString())
            ||!params.containsKey("code") || StringUtils.isBlank(params.get("code").toString())

            ) {
                return R.error("参数错误");
            }
            return  memberUserService.saveMemberUserRegister(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 用户登录
     * @param params
     * @return
     */
    @PostMapping( "/memberUser/login")
    @ResponseBody
    public R memberUserLogin(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())
                    ||!params.containsKey("password") || StringUtils.isBlank(params.get("password").toString())

            ) {
                return R.error("参数错误");
            }
            return  memberUserService.memberUserLogin(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /**
     * 企业用户补全信息
     * @param params
     * @return
     */
    @PostMapping( "/completionCompanyUser")
    @ResponseBody
    public R completionCompanyUser(@RequestBody Map<String, Object> params){

        try{
            if (
                    !params.containsKey("name") || StringUtils.isBlank(params.get("name").toString())
                    ||!params.containsKey("website") || StringUtils.isBlank(params.get("website").toString())
                    ||!params.containsKey("address") || StringUtils.isBlank(params.get("address").toString())
                    ||!params.containsKey("imgurl") || StringUtils.isBlank(params.get("imgurl").toString())
                    ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                    ||!params.containsKey("userName") || StringUtils.isBlank(params.get("userName").toString())
                    ||!params.containsKey("avator") || StringUtils.isBlank(params.get("avator").toString())
                    ||!params.containsKey("sex") || StringUtils.isBlank(params.get("sex").toString())

            ) {
                return R.error("参数错误");
            }
            return  mallCompanyUserService.completionCompanyUser(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 企业用户注册
     * @param params
     * @return
     */
    @PostMapping( "/companyUser/register")
    @ResponseBody
    public R companyUserRegister(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())
                    ||!params.containsKey("name") || StringUtils.isBlank(params.get("name").toString())
                    ||!params.containsKey("website") || StringUtils.isBlank(params.get("website").toString())
                    ||!params.containsKey("address") || StringUtils.isBlank(params.get("address").toString())
                    ||!params.containsKey("imgurl") || StringUtils.isBlank(params.get("imgurl").toString())
                    ||!params.containsKey("code") || StringUtils.isBlank(params.get("code").toString())

            ) {
                return R.error("参数错误");
            }
            return  mallCompanyUserService.companyUserRegister(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 商家入驻
     * @param params
     * @return
     */
    @PostMapping( "/shopUser/register")
    @ResponseBody
    public R shopUserRegister(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())
                    ||!params.containsKey("name") || StringUtils.isBlank(params.get("name").toString())
                    ||!params.containsKey("idcard") || StringUtils.isBlank(params.get("idcard").toString())
                    ||!params.containsKey("idcardBefore") || StringUtils.isBlank(params.get("idcardBefore").toString())
                    ||!params.containsKey("idcardAfter") || StringUtils.isBlank(params.get("idcardAfter").toString())
                    ||!params.containsKey("imgurl") || StringUtils.isBlank(params.get("imgurl").toString())
                    ||!params.containsKey("industryType") || StringUtils.isBlank(params.get("industryType").toString())
                    ||!params.containsKey("industryAttribute") || StringUtils.isBlank(params.get("industryAttribute").toString())
                    ||!params.containsKey("num") || StringUtils.isBlank(params.get("num").toString())
                    ||!params.containsKey("licenseName") || StringUtils.isBlank(params.get("licenseName").toString())
                    ||!params.containsKey("legalName") || StringUtils.isBlank(params.get("legalName").toString())
                    ||!params.containsKey("legalPhone") || StringUtils.isBlank(params.get("legalPhone").toString())
                    ||!params.containsKey("legalIdcardBefore") || StringUtils.isBlank(params.get("legalIdcardBefore").toString())
                    ||!params.containsKey("legalIdcardAfter") || StringUtils.isBlank(params.get("legalIdcardAfter").toString())
                    ||!params.containsKey("shopName") || StringUtils.isBlank(params.get("shopName").toString())
                    ||!params.containsKey("province") || StringUtils.isBlank(params.get("province").toString())
                    ||!params.containsKey("city") || StringUtils.isBlank(params.get("city").toString())
                    ||!params.containsKey("area") || StringUtils.isBlank(params.get("area").toString())
                    ||!params.containsKey("address") || StringUtils.isBlank(params.get("address").toString())
                    ||!params.containsKey("code") || StringUtils.isBlank(params.get("code").toString())


            ) {
                return R.error("参数错误");
            }
            return  mallShopUserService.shopUserRegister(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


}
